using System;
using System.Collections.Generic;
using System.Text;
using System.Xml.Serialization;

namespace WozLib.WozObjects
{

  // The right way to use a ServerObject is for every object where state needs to be saved
  // or undo/redo opertations need to use, IMPLEMENT the IServerObject interface and create
  // a mirror object inheriting from ServerObject. Any state that needs to be saved should
  // appear as a Property with an XmlInclude attribute in the ServerObject class. To avoid
  // syncranizing state, it's easiest to make the Property getter just access the IServerObject
  // state and the Setter set a local variable. Then when instantiating the IServerObject
  // (usually through the IServerObjectFactory) just use the local variables to initialize
  // the state of the IServerObject.

  // In order to not have to make the ServerObject derived class's public and have XmlIgnore attributes
  // the Getters on Properties should check whether the IServerObject is instantiated in the ObjectRegistry
  // and if not return the local variable.

  // If part of the state is pointers to or collections of other IServerObjects, then it's
  // best to have the WizardObject derived class's property Getter to actually be a reference to
  // or a collection of the equivilant ServerObject derived classes. That way when restoring from
  // XML you can just have on demand instantiation of the IServerObjects.


  //[XmlInclude(typeof(AudioToPlayServerObject))]
  //[XmlInclude(typeof(AudioPromptServerObject))]
  //[XmlInclude(typeof(StringToSpeakServerObject))]
  //[XmlInclude(typeof(ScreenServerObject))]

  // known derived class in WozLib are listed here for convience
  // external other derived classes must be registered with an 
  // xmlserializer before they can be serialized through a base class
  // as in serializing an array of ServerObjects that includes
  // derived class from outside the WozLib
  [XmlInclude(typeof(SketchPadServerObject))]
  [XmlInclude(typeof(ScreenServerObject))]
  [XmlInclude(typeof(ReplacementPadServerObject))]
  public class ServerObject
  {
    [XmlAttribute("ServerID")]
    public long ServerID
    {
      get
      {
        return _ServerID;
      }
      set
      {
        _ServerID = value;
      }
    } private long _ServerID;

    public virtual IServerObject GetIServerObject()
    {
      if (ServerObjectRegistry.ContainsObject(ServerID))
      {
        return ServerObjectRegistry.GetObject(ServerID);
      }
      else
      {
        //IServerObject iServerObject = IServerObjectFactory.FromServerObject(this);

        // don't need to make a call to the objectregistry because the objectfactory will do it for us
        //ServerObjectRegistry.RegisterObjectWithExisitingId(iServerObject);

        //return iServerObject;

        // this is broken!!!!!
        // children need to actually override this method to do the right thing
        return null;
      }
    }

    public ServerObject()
    {
    }

    public ServerObject(long serverID)
    {
      ServerID = serverID;
    }
  }
}
